package main.java.org.controller;

import lombok.extern.slf4j.Slf4j;
import main.java.Exam.pojo.Entity.*;
import main.java.Exam.pojo.tableDTO.ClassTeacherDTO;
import main.java.Exam.pojo.tableDTO.ExamDTO;
import main.java.Exam.pojo.tableDTO.ExamPageDTO;
import main.java.Exam.pojo.tableDTO.SCOREAA;
import main.java.org.mapper.ExamMapper;
import main.java.org.mapper.OnlineExamsMapper;
import main.java.org.result.Result;
import main.java.org.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam")
@Slf4j
public class ExamController {
@Autowired
ExamService examService;
@Autowired
    ExamMapper examMapper;
@Autowired
OnlineExamsMapper onlineExamsMapper;
//添加考试
@PostMapping("/addexam")
public Result add(@RequestBody ExamDTO examDTO){
    log.info("考试添加");
    examService.createxam(examDTO);

    return Result.success("考试编辑成功");
}
//老师获取自己所有考试
@GetMapping("/getexam")
public Result<ExamPageDTO> createquestion(@RequestParam String userAccount  ) {
    log.info("获取考试题目");
    //获取考试
    examService.getexam(userAccount);
  return  examService.getexam(userAccount);
}
//编辑考试
@PostMapping("/alterexam")
public Result alter(@RequestBody ExamDTO examDTO){
    log.info("考试编辑");
    examService.bianjiexam(examDTO);
    log.info("考试编辑成功");
    return Result.success("考试编辑成功");
}







//考生考试状态
    @PostMapping("/studentstatus")
    public Result getexamType(@RequestBody examType examType){
        log.info("id=:{}",examType.getId());


        List<pageexam> examStatus = onlineExamsMapper.getezamType(examType.getId());

        // 2. 包装成 Result 响应（前端能识别的格式）
        if (examStatus == null) {
            return Result.success("未查询到考试状态");
        }
        return Result.success(examStatus); // 此时 data 就是数据库返回的 examType（如“未完成”）
    }













//删除考试
@PostMapping("/deleteexam")
public Result<String> delete(@RequestBody ExamDTO examDTO ) {
    log.info("删除考试");
    examService.delete(examDTO);
    log.info("考试删除成功");
    return Result.success("考试删除成功成功");
}

//给考生所有的考试
@GetMapping("/usergetexam")
public Result<ExamPageDTO> createquestion() {
    log.info("给考生所有的考试");
    //获取考试
    examService.getuserexam();
    return  examService.getuserexam();
}



//获取单个考试
    @GetMapping("/gettestpaperdata")
    public Result<exam> getSingleExam(@RequestParam Integer testId){
    exam Exam=examService.getSigneleTest(testId);
    return Result.success(Exam);
    }
//获取题目信息
@PostMapping("/getQuestion")
public Result<List<Questtion>> getQuestion(@RequestBody List<TestQuestion> testQuestions) {
    List<Questtion> questions = examService.getQuestionsByExam(testQuestions);
    return Result.success(questions);
}

//返回给老师一场考试的学生信息
@GetMapping("/getstudentexam")
    public Result <List<ClassTeacherDTO>>getstudent(@RequestParam Integer testId){
    log.info("老师获取学生考试信息");

    List<ClassTeacherDTO> classTeacherDTO=examService.getStudentExam(testId);
    return Result.success(classTeacherDTO);
}

// AI自动批改接口
@PostMapping("/aiMark")
public Result<Integer> aiMark(@RequestBody QuestionsAi question) {
  Integer questionId= question.getQuestionId();
  String answer=question.getAnswer();
    // 调用服务层AI批改方法
    int score = examService.aiMark(questionId, answer,question.getTestId(),question.getUserAccount());
    return Result.success(score);
}



    @PostMapping("/studentscore")
    public Result <Integer>getstudent(@RequestBody SCOREAA scoreaa){
        log.info("获取学生考试成绩");
        log.info("成绩：{}",examMapper.getxx(scoreaa.getUserAccount(),scoreaa.getTestId()));
        return Result.success( examMapper.getxx(scoreaa.getUserAccount(),scoreaa.getTestId()));
    }




}
